//
//  QDFamousDoctorView.m
//  QuickDoctor
//
//  Created by 李伟 on 2016/12/20.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDFamousDoctorView.h"
#import "CLRotationView.h"

@interface QDFamousDoctorView()
@property (nonatomic, strong) NSArray<UIButton*> *btnArray;
@property (nonatomic, strong) CLRotationView *rotateView;

@end
@implementation QDFamousDoctorView

#pragma mark
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        self.rotateView.hidden = YES;
    }
    return self;
}

#pragma mark
#pragma mark - 搭建界面
- (void)setupUI
{
    UIView *tempView = [[UIView alloc]init];
    [self addSubview:tempView];
    tempView.backgroundColor = [UIColor whiteColor];
    
    //约束
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.mas_equalTo(self);
        make.height.mas_equalTo(40);
    }];
    
    //img
    UIImageView *img = [UIImageView new];
    [img setImage:[UIImage imageNamed:@"yj"]];
    [tempView addSubview:img];
    
    //约束
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tempView).mas_offset(5);
        make.left.mas_equalTo(tempView).mas_offset(10);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(30);
    }];
    
    
    //labei
    UILabel *lb_title = [[UILabel alloc]init];
    [tempView addSubview:lb_title];
    lb_title.text = @"名医通";
    lb_title.font = [UIFont systemFontOfSize:20];
    
    
    //约束
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.right.mas_equalTo(tempView);
        make.left.mas_equalTo(img.mas_right).offset(10);
        make.height.mas_equalTo(40);
    }];
    
    //btn
    NSArray *btnTitleArray = @[@"肿 瘤",@"心血管",@"神经科",@"血液科",@"骨 科",@" 公 益"];
    NSArray *btnImgArray = @[@"zl",@"xyg",@"sj",@"xy",@"gt",@"gy"];
    
    CLRotationView *rotateView = [[CLRotationView alloc] initWithFrame:CGRectMake(0, 0, 275, 275)];
    self.rotateView = rotateView;
//    rotateView.BtnBackgroudColor = [UIColor redColor];
    rotateView.layer.contents = (__bridge id)[UIImage imageNamed:@"home_center_bg"].CGImage;
    
    [rotateView BtnType:CL_RoundviewTypeCustom BtnWidth:100 adjustsFontSizesTowidth:YES masksToBounds:YES conrenrRadius:50 image:btnImgArray TitleArray:btnTitleArray titleColor:[UIColor blackColor]];
    
    [self addSubview:rotateView];
    rotateView.center = CGPointMake(self.bounds.size.width / 2+5, self.bounds.size.height / 2 );
    
#pragma mark - 使用block逆向传值，用途为点击旋转view上的按钮时弹出控制器
    __weak  typeof(self) weakself = self;
    rotateView.back = ^(NSInteger num ,NSString *name ) {
        [weakself pushView:num name:name];
    };
    
    #pragma mark - 创建中间按钮
    UIButton *midBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    midBtn.frame = CGRectMake(0, 0, 100, 100);
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView setImage:[[UIImage imageNamed:@"index_shiti.imageset"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.userInteractionEnabled = NO;
    imgView.frame = CGRectMake(midBtn.center.x - 35, midBtn.center.y-45 , 70, 70);
//    imgView.alpha = 0.3;
    [midBtn addSubview:imgView];
    
    UILabel *lable = [[UILabel alloc]init ];
    CGFloat BtnWidth = midBtn.frame.size.width;
    lable.frame = CGRectMake(imgView.center.x - (BtnWidth - 20)*0.5, CGRectGetMaxY(imgView.frame), BtnWidth - 20 , 20);
    
    lable.text = @"详 细";
    lable.textColor = [UIColor colorWithRed:0 green:0.3 blue:0 alpha:1];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:14];
    [midBtn addSubview:lable];
    
//    midBtn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    midBtn.center = rotateView.center;
    
    midBtn.layer.cornerRadius = 50;
    midBtn.alpha = 0.4;
    
    [self addSubview:midBtn];
    
    [midBtn addTarget:self action:@selector(clickToHide:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

#pragma mark
#pragma mark - 中间按钮点击
-(void)clickToHide:(UIButton *)sender
{
    if(!self.rotateView.isHidden)
    {
        [UIView animateWithDuration:.5 delay:.2 options:UIViewAnimationOptionTransitionCurlDown animations:^{
            self.rotateView.hidden = YES;
        } completion:nil];
    }
    else
    {
        [UIView animateWithDuration:.5 delay:.2 options:UIViewAnimationOptionTransitionCurlDown animations:^{
            self.rotateView.hidden = NO;
        } completion:nil];
    }
}

#pragma mark
#pragma mark - block界面跳转
- (void)pushView:(NSInteger)num name:(NSString *)name {
    
    if(num == 5)
    {
        QDCharityViewController *charityVC = [[QDCharityViewController alloc]init];
        
        charityVC.title = name;
        
        [[self getCurrentViewC].navigationController pushViewController:charityVC animated:YES];
    }
    else
    {
        QDDiseaseDetailVC *diseaseDetailVC = [[QDDiseaseDetailVC alloc]init];
        
        diseaseDetailVC.title = name;
        
        
        [[self getCurrentViewC].navigationController pushViewController:diseaseDetailVC animated:YES];
    }
    
}

#pragma mark - 获取view的当前控制器
- (UIViewController*)getCurrentViewC
{
    for (UIView * next = [self superview];next; next = next.superview) {
        
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}


//#pragma mark
//#pragma mark - 封装改变btn样式的方法
//- (void)creatBtn:(UIButton*)btn
//{
//    //使图片和文字水平居中显示
//    btn.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
//    //文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
//    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.bounds.size.height+40 ,-btn.imageView.frame.size.width-50, 0,0)];
//    //图片距离右边框距离减少图片的宽度，其它不边
//    [btn setImageEdgeInsets:UIEdgeInsetsMake(-40, 0, 0, -btn.titleLabel.bounds.size.width)];
//}

//#pragma mark
//#pragma mark - 添加btn的点击方法
//- (void)addDiseaseBtnTarget
//{
//    for (int i = 0; i < 6; i++) {
//        if (i < 5)
//        {
//            [self.btnArray[i] addTarget:self action:@selector(clickDiseaseBtn:) forControlEvents:UIControlEventTouchUpInside];
//        }
//        else
//        {
//            [self.btnArray[i] addTarget:self action:@selector(clickCharityBtn:) forControlEvents:UIControlEventTouchUpInside];
//        }
//    }
//}
//
//#pragma mark
//#pragma mark - 实现点击按钮的方法
////点击疾病
//- (void)clickDiseaseBtn:(UIButton*)button
//{
//    QDDiseaseDetailVC *diseaseDetailVC = [[QDDiseaseDetailVC alloc]init];
//
//    diseaseDetailVC.title = button.titleLabel.text;
//
//
//    [[self getCurrentViewC].navigationController pushViewController:diseaseDetailVC animated:YES];
//}
//
////点击慈善
//- (void)clickCharityBtn:(UIButton*)button
//{
//    QDCharityViewController *charityVC = [[QDCharityViewController alloc]init];
//
//    charityVC.title = button.titleLabel.text;
//
//    [[self getCurrentViewC].navigationController pushViewController:charityVC animated:YES];
//
//}
//
//#pragma mark


#pragma mark
#pragma mark - 李伟之前做的主界面6个button

//    NSMutableArray *btnArray = [NSMutableArray array];
//
//    for (int i = 0; i < btnTitleArray.count; i++) {
//
//        UIButton *btn = [[UIButton alloc]init];
//        [btn setTitle:btnTitleArray[i] forState:UIControlStateNormal];
//        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [btn setImage:[UIImage imageNamed:btnImgArray[i]] forState:UIControlStateNormal];
//        btn.backgroundColor = [UIColor whiteColor];
//
//        //改变btn样式
//        [self creatBtn:btn];
//        [self addSubview:btn];
//        [btnArray addObject:btn];
//
//        //获取宽高
//        CGFloat btnW = ([UIScreen mainScreen].bounds.size.width - 2)/3;
//        CGFloat btnH = ([UIScreen mainScreen].bounds.size.height - 64 - 290 - 42)/2;
//
//        //约束
//        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//            //上/左
//            if (i <= 2)
//            {
//                make.top.mas_equalTo(lb_title.mas_bottom).mas_offset(1);
//                make.left.mas_equalTo(self).mas_offset(btnW * i + i);
//            }
//            else
//            {
//                make.top.mas_equalTo(lb_title.mas_bottom).offset(btnH + 2);
//                make.left.mas_equalTo(self).mas_offset(btnW * (i - 3) + i-3);
//            }
//            make.width.mas_equalTo(btnW);
//            make.height.mas_equalTo(btnH);
//        }];
//
//    }
//    self.btnArray = btnArray.copy;
//批量添加btn的点击事件
//    [self addDiseaseBtnTarget];

@end
